import { _decorator, Color, Component, instantiate, Label, Node, Prefab, ScrollView, Sprite } from 'cc';
import { UIManager } from '../common/UIManager';
import { PrefabEnum } from '../common/PrefabEnum';
import ReportApi from '../api/ReportApi';
import { Report } from '../entity/Report';
import { ReportItem } from './ReportItem';
import { UIHeader } from '../common/UIHeader';
import { EventManager } from '../event/EventManager';
const { ccclass, property } = _decorator;

export enum ReportListType {
    All = 0,
    Unread = 5,
    Trade = 2,
    Help = 1,
    Attack = 3,
    Other = 4
}

@ccclass('ReportList')
export class ReportList extends Component {
    @property(Node)
    all:Node
    @property(Node)
    unread:Node
    @property(Node)
    trade:Node
    @property(Node)
    help:Node
    @property(Node)
    attack:Node
    @property(Node)
    other:Node
    @property(Node)
    content:Node
    @property(ScrollView)
    scrollView:ScrollView
    @property(Node)
    empty:Node
    @property(Prefab)
    reportItem:Prefab

    private reportListType: ReportListType
    private total:number = 1
    private page:number = 0
    private size:number = 20
    start() {
      
      

        this.all.on(Node.EventType.TOUCH_END, this.onAll, this)
        this.unread.on(Node.EventType.TOUCH_END, this.onUnread, this)
        this.trade.on(Node.EventType.TOUCH_END, this.onTrade, this)
        this.help.on(Node.EventType.TOUCH_END, this.onHelp, this)
        this.attack.on(Node.EventType.TOUCH_END, this.onAttack, this)
        this.other.on(Node.EventType.TOUCH_END, this.onOther, this)

        this.scrollView.node.on('scroll-to-top', () => {
            console.log('scroll-to-top....')
        }, this);
        this.scrollView.node.on('scroll-to-bottom', this.scrollToBottom, this);

        this.onAll()
    }

    onClose(){
        UIManager.instance.closeUI(PrefabEnum.ReportIndex)
    }
    onAll(){
        this.setActive(this.all)
        this.reportListType = ReportListType.All
        this.content.destroyAllChildren()
        this.scrollView.scrollToTop()
        this.getList(1)
    }
    onTrade(){
        this.setActive(this.trade)
        this.reportListType = ReportListType.Trade
        this.content.destroyAllChildren()
        this.scrollView.scrollToTop()
        this.getList(1)
    }
    onUnread(){
        this.setActive(this.unread)
        this.reportListType = ReportListType.Unread
        this.content.destroyAllChildren()
        this.scrollView.scrollToTop()
        this.getList(1)
    }
    onHelp(){
        this.setActive(this.help)
        this.reportListType = ReportListType.Help
        this.content.destroyAllChildren()
        this.scrollView.scrollToTop()
        this.getList(1)
    }
    onAttack(){
        this.setActive(this.attack)
        this.reportListType = ReportListType.Attack
        this.content.destroyAllChildren()
        this.scrollView.scrollToTop()
        this.getList(1)
    }
    onOther(){
       this.setActive(this.other)
       this.reportListType = ReportListType.Other
       this.content.destroyAllChildren()
       this.scrollView.scrollToTop()
       this.getList(1)
    }

    setActive(btn:Node){
        this.all.getChildByName('Label').getComponent(Label).color = new Color('#908A81')
        this.unread.getChildByName('Label').getComponent(Label).color = new Color('#908A81')
        this.trade.getChildByName('Label').getComponent(Label).color = new Color('#908A81')
        this.help.getChildByName('Label').getComponent(Label).color = new Color('#908A81')
        this.attack.getChildByName('Label').getComponent(Label).color = new Color('#908A81')
        this.other.getChildByName('Label').getComponent(Label).color = new Color('#908A81')

        btn.getChildByName('Label').getComponent(Label).color = new Color('#DACAB3')
    }

    async getList(page:number){
        this.page = page
        let param = {
            t: this.reportListType,
            page:this.page
        }
        console.log(param)
        await UIManager.instance.showLoading()
        let res = await ReportApi.execute(param)
        UIManager.instance.closeLoading()
        console.log(res)
        this.total = res.data.total
        let list: Report[] = Report.createByList(res.data.list) 
        
        for(let item of list){
            let node = instantiate(this.reportItem)
            this.content.addChild(node)
            node.getComponent(ReportItem).setData(item)
        }

        if(list.length > 0){
            this.empty.active = false
        }
    }

    scrollToBottom(){
        // if(this.page* this.size >= this.total){
        //     UIManager.instance.tip('没有更多了')
        //     return
        // }

        this.page ++
        this.getList(this.page)
    }
}


